package com.ljc.cjgl.controller;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.*;
import com.ljc.cjgl.entitiy.bbs.Article;
import com.ljc.cjgl.repository.KnowledgeRepository;
import com.ljc.cjgl.repository.bbs.ArticleRepository;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.SysUserService;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller

public class HelloController {
	@Autowired
	private ClassActivityService activityService;
	@Autowired
	private TestRegisterService testRegisterService;
	@Autowired
	private KnowledgeRepository knowledgeDAO;
	@Autowired
	private FileService fileService;
	@Autowired
	private ExperenceService experenceService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private ArticleRepository articleRepository;
	@RequestMapping("/hello")
	@ResponseBody
	public String hello() {
		return "hello world!";
	}
	
	@RequestMapping(value= { "/home","/"},method=RequestMethod.GET)
	public String inde(ModelMap map) {
		//List<TbClass> list = service.findAll();
		/*SysUser user = userUtils.getUser();
		if(user!=null) {
			String userName=(String) session.getAttribute("userName");
			if(user!=null&&!user.getName().equals(userName)) {
				session.setAttribute("userName", user.getName());
			}
		}*/
		TablePageable pageable=new TablePageable();
		pageable.setLimit(10);
		pageable.setOffset(0);
		pageable.setOrder("desc");
		//准备班级活动列表数据
		pageable.setSort("date");
		PageRequest pageRequest = pageable.bulidPageRequest();
		Page<ClassActitvity> activityPage = activityService.findAll(pageRequest);
		map.put("activities", activityPage.getContent());
		//准备作品展示列表数据 
		pageable.setSort("test.testDate");
		pageRequest=pageable.bulidPageRequest();
		Page<TestRegister> registerPage=testRegisterService.getPageByIsShow(true,pageRequest);
		List<TestRegister> registers=registerPage.getContent();
		map.put("registers",registers);
		//准备知识点列表数据
		pageable.setSort("createDate");
		pageRequest=pageable.bulidPageRequest();
		Page<Knowledge> knowledgePage=knowledgeDAO.findByOnTop(true,pageRequest);
		map.put("knowledges", knowledgePage.getContent());
		pageable.setSort("date");
		pageRequest = pageable.bulidPageRequest();
		Page<DbFile> filePage=fileService.findAll(pageRequest);
		map.put("files", filePage.getContent());
		pageable.setSort("date");
		pageRequest=pageable.bulidPageRequest();
		Page<Experence> experencePage=experenceService.findAll(pageRequest);
		map.put("experences",experencePage.getContent());
		// 准备帖子数据列表
		pageable.setSort("id");
		pageRequest = pageable.bulidPageRequest();
		Page<Article> articlePage = articleRepository.findAll(pageRequest);
		map.put("articles", articlePage.getContent());
		//map.put("list", list);
		return "home";
	}
	
	@RequestMapping(value="/login",method=RequestMethod.GET)
	public String gotoLogin() {
		return "login";
	}
	
	@RequestMapping(value="/loginSuccess")
	public String loginSuccess() {
		return "loginSuccess";
	}

	@RequestMapping(value="/loginFailure")
	public String loginFailure() {
		return "loginFailure";
	}
	/*@RequestMapping(value="/login",method=RequestMethod.POST)
	@ResponseBody
	public Object login(String userName,String pwd,HttpSession map) {
		if(userName.equals("admin")&&pwd.equals("root")) {
			Teacher admin=new Teacher();
			admin.setName("开发者");
			admin.setPwd("1243234");
			admin.setUserType(2);
			map.setAttribute("user", admin);
			return TableResult.bulidResult(true, "开发者登录成功");
		}
		Teacher teacher = teacherService.findByNameAndPwd(userName,pwd);
		if(teacher!=null) {
			map.setAttribute("user", teacher);
			return TableResult.bulidResult(true, "教师登录成功");
		}else {
			Student student=studentService.findByStuNumAndPwd(userName, pwd);
			if(student!=null) {
				map.setAttribute("user", student);
				return TableResult.bulidResult(true, "学生登录成功");
			}else {
				map.removeAttribute("user");
				return TableResult.bulidResult(false, "登录失败");
			}
		}
	}*/
	
	/*@RequestMapping("/logout")
	public String logout() {
		return "redirect:/";
	}*/
	
	@RequestMapping(value="/changePwd",method=RequestMethod.GET)
	public String gotopage() {
		return "changePwd";
	}
	
	@RequestMapping(value="/changePwd",method=RequestMethod.POST)
	public Object changePwd(String old,String pwd) {
		/*Customer user=(Customer) session.getAttribute("user");
		if(user==null||!user.getPwd().equals(old)) {
			session.removeAttribute("user");
			return TableResult.bulidResult(false, "原密码错误");
		}
		if(user.getRole().equals("学生")) {
			Student student = studentService.findById(user.getId());
			student.setPwd(pwd);
			studentService.save(student);
			session.setAttribute("user", student);
			return TableResult.bulidResult(true, "密码修改成功");
		}else {
			Teacher teacher=teacherService.findById(user.getId());
			teacher.setPwd(pwd);
			teacherService.save(teacher);
			session.setAttribute("user", teacher);
			return TableResult.bulidResult(true, "密码修改成功");
		}*/
		SysUser user=userUtils.getUser();
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		if(user==null||!encoder.matches(old, user.getPassword())) {
			return TableResult.bulidResult(false, "原密码错误");
		}else {
			user.setPassword(encoder.encode(pwd));
			sysUserService.save(user);
			return TableResult.bulidResult(true, "密码修改成功");
		}
	}
	
	@RequestMapping("/testRegisterDetails")
	public String testRegisterDetails(Integer id, ModelMap map) {
		TestRegister model=testRegisterService.findById(id);
		String demo=model.getTest().getDemo();
		demo=TableResult.txtToHtml(demo);
		model.getTest().setDemo(demo);
		map.put("model", model);
		/*String projectUrl = model.getProjectUrl();
		int index = projectUrl.lastIndexOf("/");
		projectUrl=projectUrl.substring(index+1);
		System.out.println("==========>"+projectUrl);
		model.setProjectUrl(projectUrl);*/
		return "testRegister/details";
	}
	@RequestMapping("/testRegisterMore")
	public String testRegisterMore(ModelMap map) {
		List<TbClass> tbClasses = testRegisterService.findDistinctStudentTbClass();
		List<HashMap<String, Object>> trs=new ArrayList<>();
		for (TbClass bj : tbClasses) {
			HashMap<String, Object> m=new HashMap<>();
			m.put("bj", bj.getName());
			m.put("testRegisters", testRegisterService.findByStudentTbClassId(bj.getId()));
			trs.add(m);
		}
		map.put("trs",trs);
		return "testRegister/more";
	}
	
	@RequestMapping("/knowledegDetails")
	public String knowledegDetails(Integer id,ModelMap map) {
		Knowledge model=knowledgeDAO.findById(id).get();
		map.put("model",model);
		return "knowledge/details";
	}
	
	@RequestMapping("/knowledgeMore")
	public String knowledgeMore(ModelMap map) {
		List<HashMap<String, Object>> maps=new ArrayList<>();
		List<Course> courses=courseService.findAll();
		for (Course course : courses) {
			HashMap<String, Object> m=new HashMap<>();
			m.put("course", course.getName());
			m.put("knowledges",knowledgeDAO.findByCourseId(course.getId()));
			maps.add(m);
		}
		map.put("maps",maps);
		return "knowledge/more";
	}
	
	@RequestMapping("/activityMore")
	public String activityMore(ModelMap map) {
		List<HashMap<String, Object>> maps=new ArrayList<>();
		List<TbClass> classes=activityService.findTbClass();
		for (TbClass bj : classes) {
			HashMap<String, Object> m=new HashMap<>();
			m.put("bj",bj.getName());
			m.put("activities",activityService.findByTbClassId(bj.getId()));
			maps.add(m);
		}
		map.put("maps",maps);
		return "activity/more";
	}
	
	@RequestMapping("/fileMore")
	public String fileMore(ModelMap map) {
		List<HashMap<String, Object>> maps=new ArrayList<>();
		List<Course> courses=fileService.findCourse();
		for (Course course : courses) {
			HashMap<String, Object> m=new HashMap<>();
			m.put("course",course.getName());
			m.put("files",fileService.findByCourseId(course.getId()));
			maps.add(m);
		}
		map.put("maps", maps);
		return "file/more";
	}
	
	@RequestMapping("/experenceDetails")
	public String experenceDetails(Integer id,ModelMap map) {
		Experence model=experenceService.findById(id);
		map.put("model", model);
		return "experence/details";
	}
	
	@RequestMapping("/experenceMore")
	public String experenceMore(ModelMap map) {
		List<HashMap<String, Object>> maps=new ArrayList<>();
		List<Course> courses=experenceService.findCourse();
		for (Course course : courses) {
			HashMap<String, Object> m=new HashMap<>();
			m.put("course",course.getName());
			m.put("experences",experenceService.findByCourseId(course.getId()));
			maps.add(m);
		}
		map.put("maps",maps);
		return "experence/more";
	}
}
